﻿using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Web.ModelBinding;

namespace Morus.Presentation
{
    public class ValueProviderFactoryCollection : Collection<ValueProviderFactory>
    {
        public IValueProvider GetValueProvider(ModelBindingExecutionContext modelBindingExecutionContext)
        {
            var valueProviders = from factory in ValueProviderFactories.Factories
                                 let valueProvider = factory.GetValueProvider(modelBindingExecutionContext)
                                 where valueProvider != null
                                 select valueProvider;

            return new ValueProviderCollection(valueProviders.ToList());
        }

        [DebuggerStepThrough]
        protected override void InsertItem(int index, ValueProviderFactory item)
        {
            Check.Argument.IsNotNull(item, "item");

            base.InsertItem(index, item);
        }

        [DebuggerStepThrough]
        protected override void SetItem(int index, ValueProviderFactory item)
        {
            Check.Argument.IsNotNull(item, "item");

            base.SetItem(index, item);
        }
    }
}
